﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfaces__Indexes__Operators
{
    class ComplexNumber
    {
        public long Real { get; private set; }
        public long Imaginary { get; private set; }

        public ComplexNumber(long real, long imaginary)
        {
            this.Real = real;
            this.Imaginary = imaginary;
        }

        public static ComplexNumber operator +(ComplexNumber f1, ComplexNumber f2)
        {
            long real = f1.Real + f2.Real;
            long imaginary = f1.Imaginary + f2.Imaginary;

            return new ComplexNumber(real, imaginary);
        }

        public static ComplexNumber operator -(ComplexNumber f1, ComplexNumber f2)
        {
            long real = f1.Real - f2.Real;
            long imaginary = f1.Imaginary - f2.Imaginary;

            return new ComplexNumber(real, imaginary);
        }

        public static ComplexNumber operator *(ComplexNumber f1, ComplexNumber f2)
        {
            long real = (f1.Real * f2.Real) - (f1.Imaginary * f2.Imaginary);
            long imaginary = (f1.Imaginary * f2.Real) - (f1.Real * f2.Imaginary);

            return new ComplexNumber(real, imaginary);
        }

        public static ComplexNumber operator /(ComplexNumber f1, ComplexNumber f2)
        {
            long real = (((f1.Real * f2.Real) + (f1.Imaginary * f2.Imaginary)) / 
                (f2.Real * f2.Real) + (f2.Imaginary * f2.Imaginary));

            long imaginary = ((f1.Imaginary * f2.Real) - (f1.Real * f2.Imaginary)) / 
                ((f2.Real * f2.Real) + (f2.Imaginary * f2.Imaginary));

            return new ComplexNumber(real, imaginary);
        }

        public override string ToString()
        {
            return String.Format("{0} + {1}*i", Real, Imaginary);
        }

        public long this[int index]
        {
            get
            {
                if (index == 0)
                {
                    return this.Real;
                }
                else if (index == 1)
                {
                    return this.Imaginary;
                }
                else
                {
                    throw new IndexOutOfRangeException("The allowed indexes are 0 for real or 1 for imaginary");
                }
            }
        }
    }
}
